﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualBasic;
using System.CodeDom.Compiler;

namespace CodeScrapWindow
{
	/// <summary>
	/// Provides VB.NET compiler functionality.
	/// </summary>
	internal class VBCompilerProvider : ICompilerProvider
	{
		/// <summary>
		/// Gets the name of the compiler to show to the user.
		/// </summary>
		/// <value></value>
		public string Name
		{
			get
			{
				return "VB";
			}
		}

		/// <summary>
		/// Gets the language service GUID to use for syntax coloring.
		/// </summary>
		/// <value>The language service GUID.</value>
		public string LanguageServiceGuid
		{
			get
			{
				// TODO: This doesn't work; it appears to get set correctly but nothing gets colored and it errors on backspace
				//return "E34ACDC0-BAAE-11D0-88BF-00A0C9110049";
				return "";
			}
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="VBCompilerProvider"/> class.
		/// </summary>
		public VBCompilerProvider()
		{
		}

		/// <summary>
		/// Builds the class code from the code entered by the user.
		/// </summary>
		/// <param name="imports">The namespaces to add as import/using statements.</param>
		/// <param name="code">The code entered by the user.</param>
		/// <returns>
		/// A complete, compilable class with the namespace, class name and method name coming from the <see cref="CodeScrapWindow.Constants"/> class.
		/// </returns>
		public string BuildCode(IEnumerable<string> imports, string code)
		{
			StringBuilder classCode = new StringBuilder();

			foreach (string import in imports)
			{
				classCode.AppendFormat("Imports {0}", import);
				classCode.AppendLine();
			}

			classCode.AppendLine();
			classCode.AppendLine("Namespace " + Constants.GeneratedNamespace);
			classCode.AppendLine("    NotInheritable Class " + Constants.GeneratedClassName);
			classCode.AppendLine("        Public Sub " + Constants.GeneratedMethodName + "()");

			classCode.AppendLine(code);

			classCode.AppendLine("        End Sub");
			classCode.AppendLine("    End Class");
			classCode.AppendLine("End Namespace");

			return classCode.ToString();
		}

		/// <summary>
		/// Gets the language-specific DOM provider to use for compiling code.
		/// </summary>
		/// <returns>The CodeDomProvider to use for compiling code.</returns>
		public CodeDomProvider GetCompiler()
		{
			return new VBCodeProvider();
		}
	}
}
